/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "gpdma.h"
#include "i2c.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include "bsp_sht20.h"
#include "bsp_ili9341_4line.h"
#include "wifi.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint8_t USART5_RxBuff[1024]={0};
volatile uint8_t USART5_RxCounter=0;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

char displayBuf[12][32] = {0};  // 显示缓冲区（12行）
int tempHigh = 33;             // 温度上限
int tempLow = 31;              // 温度下限
int humHigh = 60;              // 湿度上限
int humLow = 40;               // 湿度下限

uint32_t VCC;               //电压数据
SHT20_TemRH_Val temp;       //温湿度数据

// 工作模式枚举（仅保留自动和手动）
typedef enum {
    AUTO_MODE,       // 自动模式
    MANUAL_MODE      // 手动模式
} WorkMode_t;

// 全局变量
int currentMode = 0; 
extern int connect_flag;

// 按键消抖相关变量
uint8_t keyState = 0;      // 按键状态(0-未按, 1-按下)
uint8_t selectedLine = 0;  // 当前选中的行(0-11)
uint8_t manualControlFlags = 0; // 手动控制标志

// 设备状态（更新名称和引脚关联）
char dryStatus[8] = "OFF";      // 除湿器状态(Dry，对应PC6)
char moisyStatus[8] = "OFF";    // 加湿器状态(Moisy，对应PC7)
char coolerStatus[8] = "OFF";   // 制冷器状态(PC4)
char heaterStatus[8] = "OFF";   // 加热器状态(PC13)
char buzzerStatus[8] = "OFF";   // 蜂鸣器状态(PA15)
char modeStatus[16] = "AUTO_MODE"; 

void updateDeviceStatus(void);   
void autoControlMode(void);
void manualmode(void);
void handleManualModeKeyPress(uint8_t key);
void updateSelectedLineDisplay(void);
void ILI9341();                   
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size);
void changeMode(void);

// 重定向printf到UART
int fputc(int ch, FILE * p)
{   
    while (!(UART5->ISR & (1<<7)));  
    UART5->TDR = ch;                 
    return 0;
}

int fgetc(FILE *p)
{
    while(!(USART1->ISR & (1<<5))){}
    return USART1->RDR;
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* Configure the System Power */
  SystemPower_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();          // 确保GPIO配置：PC6(Dry), PC7(Moisy), PA15(蜂鸣器)
  MX_GPDMA1_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  MX_TIM6_Init();
  MX_I2C1_Init();
  MX_TIM7_Init();
  MX_ADC1_Init();
  MX_UART5_Init();
  /* USER CODE BEGIN 2 */
  HAL_UARTEx_ReceiveToIdle_IT(&huart5, USART5_RxBuff, 1024); // 开启空闲接收
  WIFI_Connect();
  HAL_TIM_Base_Start_IT(&htim6);
  HAL_TIM_Base_Start_IT(&htim7);
  ILI9341_Init(); 
  HAL_PWREx_EnableVddA();        // 启动VDDA电压
  HAL_PWREx_EnableVddIO2();      // 启用VDDIO电压
  HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); // 校准ADC
  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {  
    // 启动ADC_DMA采样电压（主循环中执行）
    HAL_ADC_Start_DMA(&hadc1, &VCC, 1);
    HAL_Delay(500);
    HAL_ADC_Stop_DMA(&hadc1);
      
    // 自动模式下执行控制逻辑
    if (currentMode == AUTO_MODE && (VCC * 3.3 / 16384) != 0) {
        autoControlMode();
    }
      
    ILI9341();     // 显示屏刷新 

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_0;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV4;
  RCC_OscInitStruct.PLL.PLLM = 3;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 1;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1;
  RCC_OscInitStruct.PLL.PLLFRACN = 0;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief Power Configuration
  * @retval None
  */
static void SystemPower_Config(void)
{
  HAL_PWREx_DisableUCPDDeadBattery();
}

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{    
    if (connect_flag == 1 && htim == &htim6) {
        BSP_SHT20_GetData();
        printf("===== 设备状况数据上传=====\n");
        printf("温度: %.2f°C 湿度: %.2f%%\n", temp.Tem, temp.Hum);
        printf("电压: %.2fV\n", VCC * 3.3 / 16384);
        printf("设备状况:\n");
        printf("除湿器: %s\n", dryStatus);      // 更新名称
        printf("加湿器: %s\n", moisyStatus);  // 更新名称
        printf("制冷器状态: %s\n", coolerStatus);
        printf("加热器状态: %s\n", heaterStatus);
        printf("蜂鸣器: %s\n", buzzerStatus);
        printf("工作模式: %s\n", modeStatus);
        printf("温度阈值: %d°C~%d°C\n", tempLow, tempHigh);
        printf("湿度阈值: %d%%~%d%%\n", humLow, humHigh);
        printf("==============================\n");
    }
}

void ILI9341(){
  // 格式化显示内容（更新设备名称）
  sprintf(displayBuf[0], "Temp:%.2f Hum:%.1f%%", temp.Tem, temp.Hum);
  sprintf(displayBuf[1], "T_Low: %d", tempLow);
  sprintf(displayBuf[2], "T_High: %d", tempHigh);
  sprintf(displayBuf[3], "H_Low: %d%%", humLow);
  sprintf(displayBuf[4], "H_High: %d%%", humHigh);
  sprintf(displayBuf[5], "VCC:%.2fV", VCC * 3.3 / 16384);
  sprintf(displayBuf[6], "Dry: %s", dryStatus);      // 除湿器(Dry)
  sprintf(displayBuf[7], "Moisy: %s", moisyStatus);  // 加湿器(Moisy)
  sprintf(displayBuf[8], "Cooler: %s", coolerStatus);
  sprintf(displayBuf[9], "Heater: %s", heaterStatus);
  sprintf(displayBuf[10], "Buzzer: %s", buzzerStatus);
  sprintf(displayBuf[11], "Mode: %s", modeStatus);
  
  updateSelectedLineDisplay();
} 

/**
  * @brief  自动控制模式（除湿器引脚改为PC6）
  */
void autoControlMode(void)
{
    uint8_t isAlarm = 0; // 报警标志：0-正常，1-异常
    
    // 温度控制逻辑
    if (temp.Tem > tempHigh) {
        // 温度过高：开制冷器，关加热器
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 1);   // 制冷器PC4
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);  // 加热器PC13
        strcpy(coolerStatus, "ON ");
        strcpy(heaterStatus, "OFF");
        isAlarm = 1; // 触发报警
    } else if (temp.Tem < tempLow) {
        // 温度过低：开加热器，关制冷器
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);  // 加热器PC13
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 0);   // 制冷器PC4
        strcpy(heaterStatus, "ON ");
        strcpy(coolerStatus, "OFF");
        isAlarm = 1; // 触发报警
    } else {
        // 温度正常：关闭制冷/加热器
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 0);
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
        strcpy(coolerStatus, "OFF");
        strcpy(heaterStatus, "OFF");
    }
    
    // 湿度控制逻辑（除湿器改为PC6）
    if (temp.Hum > humHigh) {
        // 湿度过高：开Dry(除湿)，关Moisy(加湿)
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 1);   // 除湿器PC6
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 0);   // 加湿器PC7
        strcpy(dryStatus, "ON ");
        strcpy(moisyStatus, "OFF");
        isAlarm = 1; // 触发报警
    } else if (temp.Hum < humLow) {
        // 湿度过低：开Moisy(加湿)，关Dry(除湿)
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 1);   // 加湿器PC7
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 0);   // 除湿器PC6
        strcpy(moisyStatus, "ON ");
        strcpy(dryStatus, "OFF");
        isAlarm = 1; // 触发报警
    } else {
        // 湿度正常：关闭除湿/加湿器
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 0);   // 除湿器PC6
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 0);   // 加湿器PC7
        strcpy(dryStatus, "OFF");
        strcpy(moisyStatus, "OFF");
    }
    
    // 蜂鸣器控制（异常时报警）
    if (isAlarm) {
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);  // 蜂鸣器PA15
        strcpy(buzzerStatus, "ON ");
    } else {
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);
        strcpy(buzzerStatus, "OFF");
    }
}

/**
  * @brief  手动模式
  */
void manualmode(void){
    // 手动模式初始关闭所有设备
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 0);    // 制冷器
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);   // 加热器
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 0);    // 除湿器(Dry) PC6
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 0);    // 加湿器(Moisy) PC7
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);   // 蜂鸣器
    
    // 更新状态显示
    strcpy(coolerStatus, "OFF");
    strcpy(heaterStatus, "OFF");
    strcpy(dryStatus, "OFF");
    strcpy(moisyStatus, "OFF");
    strcpy(buzzerStatus, "OFF");
    
    // 重置选中行
    selectedLine = 0;
    updateSelectedLineDisplay();
}

/**
  * @brief  处理手动模式下的按键按下
  * @param  key 按键编号(1-3)
  */
void handleManualModeKeyPress(uint8_t key)
{
    if (currentMode != MANUAL_MODE) return;
    
    switch (key) {
        case 1: // KEY1 - 切换选中行
            selectedLine = (selectedLine + 1) % 12;
            updateSelectedLineDisplay();
            break;
            
        case 2: // KEY2 - 增加/开启
            if (selectedLine >= 1 && selectedLine <= 4) { // 阈值设置行
                switch (selectedLine) {
                    case 1: if (tempLow < tempHigh - 1) tempLow++; break;
                    case 2: tempHigh++; break;
                    case 3: if (humLow < humHigh - 1) humLow++; break;
                    case 4: humHigh++; break;
                }
            } 
            else if (selectedLine >= 6 && selectedLine <= 10) { // 设备控制行
                switch (selectedLine) {
                    case 6:  // Dry(除湿器)
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 1);  // PC6
                        strcpy(dryStatus, "ON ");
                        break;
                    case 7:  // Moisy(加湿器)
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 1);  // PC7
                        strcpy(moisyStatus, "ON ");
                        break;
                    case 8:  // 制冷器
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 1);   // PC4
                        strcpy(coolerStatus, "ON ");
                        break;
                    case 9:  // 加热器
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);  // PC13
                        strcpy(heaterStatus, "ON ");
                        break;
                    case 10: // 蜂鸣器
                        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1);  // PA15
                        strcpy(buzzerStatus, "ON ");
                        break;
                }
            }
            ILI9341(); // 刷新显示
            break;
            
        case 3: // KEY3 - 减少/关闭
            if (selectedLine >= 1 && selectedLine <= 4) { // 阈值设置行
                switch (selectedLine) {
                    case 1: if (tempLow > -40) tempLow--; break;
                    case 2: if (tempHigh > tempLow + 1) tempHigh--; break;
                    case 3: if (humLow > 0) humLow--; break;
                    case 4: if (humHigh > humLow + 1) humHigh--; break;
                }
            } 
            else if (selectedLine >= 6 && selectedLine <= 10) { // 设备控制行
                switch (selectedLine) {
                    case 6:  // Dry(除湿器)
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 0);  // PC6
                        strcpy(dryStatus, "OFF");
                        break;
                    case 7:  // Moisy(加湿器)
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 0);  // PC7
                        strcpy(moisyStatus, "OFF");
                        break;
                    case 8:  // 制冷器
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 0);   // PC4
                        strcpy(coolerStatus, "OFF");
                        break;
                    case 9:  // 加热器
                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);  // PC13
                        strcpy(heaterStatus, "OFF");
                        break;
                    case 10: // 蜂鸣器
                        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0);  // PA15
                        strcpy(buzzerStatus, "OFF");
                        break;
                }
            }
            ILI9341(); // 刷新显示
            break;
    }
}

/**
  * @brief  更新选中行的显示效果
  */
void updateSelectedLineDisplay(void)
{
    // 显示所有行
    for (int i = 0; i < 12; i++) {
        if (i == selectedLine && currentMode == MANUAL_MODE) {
            // 手动模式下高亮显示选中行
            Gui_DrawFont_GBK16(20, 20 + i * 20, RED, BLACK, displayBuf[i]);
        } else {
            // 其他行正常显示
            Gui_DrawFont_GBK16(20, 20 + i * 20, WHITE, BLACK, displayBuf[i]);
        }
    }
}

// 模式切换（仅在自动和手动模式间切换）
void changeMode(void)
{
    currentMode = (currentMode + 1) % 2; // 两种模式循环切换
    
    // 更新模式状态显示
    switch (currentMode) {
        case AUTO_MODE:
            strcpy(modeStatus, "AUTO_MODE");
            autoControlMode();
            break;
        case MANUAL_MODE:
            strcpy(modeStatus, "MANUAL_MODE");
            manualmode();
            break;
    }
    ILI9341(); 
}

void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{   
    if (GPIO_Pin == GPIO_PIN_12) {     // 模式切换按键
        changeMode();
    }
    else if (currentMode == MANUAL_MODE) { // 手动模式下处理按键
        if (GPIO_Pin == GPIO_PIN_9) {      // KEY1
            handleManualModeKeyPress(1);
        }
        else if (GPIO_Pin == GPIO_PIN_8) { // KEY2
            handleManualModeKeyPress(2);
        }
        else if (GPIO_Pin == GPIO_PIN_5) { // KEY3
            handleManualModeKeyPress(3);
        }
    }
}

// WIFI中断回调函数（更新设备控制指令）
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
    if(huart == &huart5)
    {
        USART5_RxCounter=1;
        if(connect_flag == 1)
        {
            u1_USART("server:%s\n", USART5_RxBuff);
            strcpy(modeStatus, "MANUAL_MODE");
            currentMode = MANUAL_MODE;
            // 设备控制指令（更新名称和引脚）
            if (strcmp(USART5_RxBuff, "DRY_ON") == 0 && currentMode == MANUAL_MODE) {
                strcpy(dryStatus, "ON ");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 1); // PC6
            }
            if (strcmp(USART5_RxBuff, "DRY_OFF") == 0 && currentMode == MANUAL_MODE) {
                strcpy(dryStatus, "OFF");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, 0); // PC6
            }
            if (strcmp(USART5_RxBuff, "MOISY_ON") == 0 && currentMode == MANUAL_MODE) {
                strcpy(moisyStatus, "ON ");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 1); // PC7
            }
            if (strcmp(USART5_RxBuff, "MOISY_OFF") == 0 && currentMode == MANUAL_MODE) {
                strcpy(moisyStatus, "OFF");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, 0); // PC7
            }
            if (strcmp(USART5_RxBuff, "COOLER_ON") == 0 && currentMode == MANUAL_MODE) {
                strcpy(coolerStatus, "ON ");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 1); // PC4
            }
            if (strcmp(USART5_RxBuff, "COOLER_OFF") == 0 && currentMode == MANUAL_MODE) {
                strcpy(coolerStatus, "OFF");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, 0); // PC4
            }
            if (strcmp(USART5_RxBuff, "HEATER_ON") == 0 && currentMode == MANUAL_MODE) {
                strcpy(heaterStatus, "ON ");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1); // PC13
            }
            if (strcmp(USART5_RxBuff, "HEATER_OFF") == 0 && currentMode == MANUAL_MODE) {
                strcpy(heaterStatus, "OFF");
                HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0); // PC13
            }
            if (strcmp(USART5_RxBuff, "BUZZER_ON") == 0 && currentMode == MANUAL_MODE) {
                strcpy(buzzerStatus, "ON ");
                HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); // PA15
            }
            if (strcmp(USART5_RxBuff, "BUZZER_OFF") == 0 && currentMode == MANUAL_MODE) {
                strcpy(buzzerStatus, "OFF");
                HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); // PA15
            }
            
            // 模式切换指令
            if (strcmp(USART5_RxBuff, "AUTO_MODE") == 0) {
                strcpy(modeStatus, "AUTO_MODE");
                currentMode = AUTO_MODE;
                autoControlMode();
            }
            if (strcmp(USART5_RxBuff, "MANUAL_MODE") == 0) {
                strcpy(modeStatus, "MANUAL_MODE");
                currentMode = MANUAL_MODE;
                manualmode();
            }
            
            // 阈值调整指令（仅手动模式有效）
            if (strcmp(USART5_RxBuff, "tempHighUp") == 0 && currentMode == MANUAL_MODE)
                tempHigh += 1;
            if (strcmp(USART5_RxBuff, "tempHighDown") == 0 && currentMode == MANUAL_MODE)
                tempHigh -= 1;
            if (strcmp(USART5_RxBuff, "tempLowUp") == 0 && currentMode == MANUAL_MODE)
                tempLow += 1;
            if (strcmp(USART5_RxBuff, "tempLowDown") == 0 && currentMode == MANUAL_MODE)
                tempLow -= 1;
            if (strcmp(USART5_RxBuff, "humHighUp") == 0 && currentMode == MANUAL_MODE)
                humHigh += 1;
            if (strcmp(USART5_RxBuff, "humHighDown") == 0 && currentMode == MANUAL_MODE)
                humHigh -= 1;
            if (strcmp(USART5_RxBuff, "humLowUp") == 0 && currentMode == MANUAL_MODE)
                humLow += 1;
            if (strcmp(USART5_RxBuff, "humLowDown") == 0 && currentMode == MANUAL_MODE)
                humLow -= 1;
            
            memset(USART5_RxBuff, 0, 1024);
        }
        HAL_UARTEx_ReceiveToIdle_IT(&huart5, USART5_RxBuff, 1024);
    }
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  printf("Wrong parameters value: file %s on line %d\r\n", file, line);
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */